package com.example.administrator.test.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.example.administrator.test.PoiSearchActivity;
import com.example.administrator.test.R;
import com.example.administrator.test.overlayutil.PoiOverlay;

import utils.FixPosition;
import utils.MySkipPoiSearch;
import utils.ShowNearCar;

/**
 * Created by Administrator on 2016/11/18.
 */
public class FragmentDrivingService extends Fragment {
    private BaiduMap baiduMap;
    private TextureMapView mapView;
    private GeoCoder geoCoder;//地理编码检索类
    private TextView tv_fragment_drivingservice_start,tv_fragment_drivingservice_end;//行程开始、结束定位地点
    private String sStartLongitude,sStartLatitude, sEndLongitude, sEndLatitude;//开始、结束地点经纬度  String类型
    private double dStartLatitude, dStartLongitude, dEndLongitude, dEndLatitude;//开始、结束地点经纬度  double类型
    private String locPlace,endAdress;//开始地点的名称
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        SDKInitializer.initialize(getActivity());
        View view=inflater.inflate(R.layout.fragment_drivingservice,null);
        mapView= (TextureMapView) view.findViewById(R.id.driving_service_mapview);
        mapView.showZoomControls(false);//不显示缩放控件
        geoCoder = GeoCoder.newInstance();//创建地理编码检索实例
        tv_fragment_drivingservice_start= (TextView) view.findViewById(R.id.tv_fragment_drivingservice_start);
        tv_fragment_drivingservice_end= (TextView) view.findViewById(R.id.tv_fragment_drivingservice_end);
        //调用点击结束地点跳转到poiSearchActivity的方法
        MySkipPoiSearch.MySkipPoiSearch(getActivity(),tv_fragment_drivingservice_end);
        Intent intent=getActivity().getIntent();
        endAdress=intent.getStringExtra("endAdress");
        tv_fragment_drivingservice_end.setText(endAdress);
        baiduMap=mapView.getMap();
        //调用定位的方法
        FixPosition.FixPosition(mapView,baiduMap,getContext(),geoCoder,tv_fragment_drivingservice_start);
        //获取开始地点名称
        locPlace=FixPosition.getLocPlace();
        //结束地点经纬度  double型
        dEndLongitude =PoiSearchActivity.getLongitude();
        dEndLatitude =PoiSearchActivity.getLatitude();
        //结束地点经纬度  String型
        sEndLongitude=dEndLongitude+"";
        sEndLatitude=dEndLatitude+"";
        //开始地点经纬度  String型
        dStartLongitude =FixPosition.getStartLongitude();
        dStartLatitude =FixPosition.getStartLatitude();
        //开始地点经纬度  double型
        sStartLongitude= dStartLongitude +"";
        sStartLatitude= dStartLatitude +"";
        //调用显示附近车辆的方法
//            ShowNearCar.ShowNearCar(baiduMap, startLatitude1, startLongitude1);
        return view;
    }
    @Override
    public void onPause() {
        mapView.onPause();
        super.onPause();
    }

    @Override
    public void onResume() {
        mapView.onResume();
        //实现定位功能
        FixPosition.FixPosition(mapView,baiduMap,getContext(),geoCoder,tv_fragment_drivingservice_start);
        super.onResume();
    }

    @Override
    public void onDestroy() {
        //退出时销毁定位
        //关闭定位图层
        baiduMap.setMyLocationEnabled(false);
        mapView.onDestroy();
        mapView=null;
        super.onDestroy();
    }

}
